package cn.nfwx.tv.manager;

import android.content.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nfwx.tv.callback.ResponseListener;
import cn.nfwx.tv.conts.Constants;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.okhttp.CustomOkHttpClient;
import cn.nfwx.tv.okhttp.UrlHelper;

/**
 * Created by dell on 2017/11/24.
 */

public class WatchRecordManager {
    private static String list_url = Constants.test_url + "/nfwx-user/v1/history?";
    private static String add_url = Constants.test_url + "/nfwx-user/v1/history?";
    private static String delete_url = Constants.test_url + "/nfwx-user/v1/history?";

    /***
     * 观看记录列表
     * @param context
     * @param pageNo
     * @param pageSize
     * @param callBack
     */
//    public static void requestWatchRecordList(Context context, int pageNo, int pageSize, String header, WatchRecordCallBack callBack) {
//        Log.d("url", list_url + "page=" + pageNo + "&per_page=" + pageSize);
//        CustomOkHttpClient.getObjectRequestWithHeader(context, list_url + "page=" + pageNo + "&per_page=" + pageSize, "WatchRecordManager", header, callBack);
//    }

    /***
     * 删除观看记录
     * @param context
     * @param programIds
     * @param callBack
     */
    public static void deleteWatchRecord(Context context, String programIds, String header, ResponseListener<String> callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("vid", programIds);
        CustomOkHttpClient.deleteObjectRequestWithBody(context, delete_url, programIds, "WatchRecordManager", header, callBack);
    }

    /***
     * 添加观看记录
     * @param context
     * @param vid
     * @param header
     * @param name
     * @param percent
     * @param position
     * @param timeCode
     * @param duration
     * @param type
     * @param watchTime
     * @param callBack
     */
    public static void addWatchRecord(Context context, String vid, String name, String percent,
                                      String position, String timeCode, String duration, String type, String watchTime, String header, ResponseListener<String> callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("vid", vid);
        params.put("name", name);
        params.put("percent", percent);
        params.put("position", position);
        params.put("timeCode", timeCode);
        params.put("duration", duration);
        params.put("type", type);
        params.put("watchTime", watchTime);
        CustomOkHttpClient.postObjectRequestWithHeader(context, add_url, params, Constants.TIMEOUT, "addWatchRecord", header, callBack);

    }

    /**
     * 获取观看记录
     *
     * @param context
     * @param responseListener
     * @return
     */
    public static String requestViewHistroyList(Context context, ResponseListener responseListener) {
        String requestTag = "requestViewHistroyList";
        String url = UrlHelper.getIjkViewHistroyUrl(AccountHelper.getInstance().getUid(context));
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, requestTag, AccountHelper.getInstance().getToken(context), responseListener);
        return requestTag;
    }

    /**
     * 设置观看记录
     *
     * @param context
     * @param programId
     * @param position
     * @param percent
     * @return
     */
    public static String requestAddViewHistroy(Context context, int programId, long position, int percent, ResponseListener responseListener) {
        String request_tag = "requestAddViewHistroy";
        String url = UrlHelper.getAddViewHistroyUrl(AccountHelper.getInstance().getUid(context), programId, position, percent);
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }

    /**
     * 删除观看记录
     *
     * @param context
     * @param programIds
     * @param responseListener
     * @return
     */
    public static String requestDelViewHistroy(Context context, List<Integer> programIds, ResponseListener responseListener) {
        String request_tag = "requestDelViewHistroy";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < programIds.size(); i++) {
            sb.append(programIds.get(i) + ",");
        }
        String newStr = sb.toString();
        String str3 = newStr.substring(0, newStr.length() - 1);
        String url = UrlHelper.getDelViewHistroyUrl(AccountHelper.getInstance().getUid(context), str3);
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }
}
